<template>
  <div class="info-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch"></search-tool>
      <el-button @click="handleAdd" type="primary" style="margin-bottom: 24px"
        >新增工单</el-button
      >
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        height="75%"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column prop="ticketsRegulateForm.no" label="工单编号">
        </el-table-column>
        <el-table-column prop="ticketsRegulateForm.remark" label="工单描述">
        </el-table-column>
        <el-table-column prop="pipeInfoName" label="所属管廊">
        </el-table-column>
        <el-table-column prop="warehouseName" label="所属仓室">
        </el-table-column>
        <el-table-column prop="subregionName" label="防火分区" width="150">
        </el-table-column>
        <el-table-column prop="ticketsRegulateForm.unit" label="所属单位">
        </el-table-column>
        <el-table-column prop="ticketsRegulateForm.type" label="工单类型">
        </el-table-column>
        <el-table-column prop="ticketsRegulateForm.principal" label="负责人">
        </el-table-column>

        <el-table-column prop="ticketsRegulateForm.result" label="处理结果">
        </el-table-column>
        <el-table-column prop="ticketsRegulateForm.startTime" label="开始时间">
        </el-table-column>
        <el-table-column prop="ticketsRegulateForm.endTime" label="结束时间">
        </el-table-column>
        <el-table-column prop="createTime" label="登记时间" width="150">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <div class="action-row">
              <svg-icon icon="edit" @click.prevent="handleEdit(scope.row)" />
              <div class="has-more"></div>
              <svg-icon icon="dele" @click="handleDel(scope.row)" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <add-dialog
        :title="dialogTitle"
        :rowData="curRowData"
        v-model:visible="visibleDialog"
        @onReload="reloadPage"
        @onCloes="onCloes"
        ref="dialogClear"
      ></add-dialog>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import { nextTick, ref, watch } from 'vue'
import SearchTool from './components/SearchToll/index.vue'
import AddDialog from './components/AddDialog/index.vue'
import { ElMessageBox } from 'element-plus'
import { workorderList, workorderDel, getNewCode } from '@/api/back'
import { message } from '@/utils/message'
//数据相关
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(8)
const visibleDialog = ref(false)
const curRowData = ref({})
const dialogTitle = ref('')
const params = ref({})
const loading = ref(false)
const ticketsRegulateForm = ref()
const dialogClear = ref(null)
//获取数据的方法
const getListData = async (params) => {
  loading.value = true
  const result = await workorderList(params, page.value, size.value)

  loading.value = false
  tableData.value = result.rows
  total.value = result.total
}
getListData(params.value)

const deleList = async (id) => {
  const res = await workorderDel(id)
  console.log(res)
  message({ type: 'success', message: '删除成功!' })
  getListData(params.value)
}
const reloadPage = () => {
  getListData(params.value)
  if (visibleDialog.value) {
    visibleDialog.value = false
  }
}
reloadPage()

const handleSearch = (paramsData) => {
  params.value = paramsData
  console.log(paramsData)
  let obj = {
    startTime: paramsData.time[0],
    endTime: paramsData.time[1],
    remark: paramsData.remark,
    type: paramsData.type
  }
  console.log(obj)
  getListData(obj)
}
const onCloes = () => {
  visibleDialog.value = false
  // curRowData.value = {}
}
// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}
/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}
//新增自动编码
const gecode = async () => {
  const r = await getNewCode()
  return r
}
const handleAdd = async () => {
  let a = await gecode()
  dialogTitle.value = '新建工单'
  curRowData.value = {
    id: '',
    ticketsRegulateForm: {
      no: a.no,
      remark: '',
      unit: '',
      type: '',
      startTime: '',
      endTime: '',
      principal: '',
      result: ''
    },
    pipeId: '',
    warehouseId: '',
    subregionId: '',
    createTime: ''
  }
  visibleDialog.value = true
  nextTick(() => {
    dialogClear.value.cleara()
  })
}
const handleEdit = (scoe) => {
  visibleDialog.value = true
  dialogTitle.value = '编辑工单'
  curRowData.value = scoe
  dialogClear.value.cleara()
  dialogClear.value.morePont()
}
// 删除信息
const handleDel = (row) => {
  console.log(row)
  ElMessageBox.confirm('确定删除此工单?', '删除工单', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      // 删除
      deleList(row.ticketsRegulateForm.id)
      console.log(row.ticketsRegulateForm.id)
    })
    .catch(() => {})
}
</script>

<style lang="scss" scoped>
// :deep(.el-input__inner) {
//   width: 212px;
// }
.info-container {
  width: 100%;
  height: 100%;
  .action-row {
    display: flex;
    .has-more {
      width: 2px;
      height: 10px;
      margin-right: 10px;
      margin-left: 10px;
      margin-top: 5px;
    }
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
</style>
